package hu.elte.inf.holiuml.coordgeom;

import java.awt.FontMetrics;
import java.awt.Graphics;

public class TextUtils {
	private Graphics g;
	private FontMetrics fontMetrics;
	
	public TextUtils(Graphics g) {
		this.g = g;
		this.fontMetrics = g.getFontMetrics();
	}
	
	public int lineHeight() {
		return fontMetrics.getMaxAscent() + fontMetrics.getMaxDescent();
	}
	
	public int maxAscent() {
		return fontMetrics.getMaxAscent();
	}
	
	public int stringWidth(String s) {
		return fontMetrics.stringWidth(s);
	}
	
	public CGPoint centerToUpperLeft(CGPoint p, String s) {
		int x = p.getX() - stringWidth(s)/2;
		int y = p.getY() - lineHeight()/2; 
		return new CGPoint(x, y);
	}

	public void drawString(String s, CGPoint p) {
		g.drawString(s, p.getX(), p.getY() + maxAscent());
	}

	public void drawStringCenter(String s, CGPoint centerPoint) {
		drawString(s, centerToUpperLeft(centerPoint, s));
	}
			


}
